<?php

class SiteController extends HomeController
{
    /**
     * Ham chia mang thanh n mang con
     * @author :SonHA
     * @date : 7/5/2013
     * @param $array : mang ban dau
     * @param $segmentCount : so mang con can chia
     * @return array|bool
     */
    function array_divide($array, $segmentCount)
    {
        $dataCount = count($array);
        if ($dataCount == 0) return false;
        $segmentLimit = ceil($dataCount / $segmentCount);
        $outputArray = array_chunk($array, $segmentLimit);

        return $outputArray;
    }

    public function actionIndex()
    {
        $this->pageTitle = 'Thời trang công sở nữ - thanh lịch, đơn giản, nổi bật';
        $lastest = Product::model()->getListNewProduct();
        $feature = Product::model()->getListFeatureProduct();
        $this->render('home', array(
            'lastest' => $lastest,
            'feature' => $feature,
        ));
    }

    public function actionSearch()
    {
        $key = isset($_REQUEST['keywords']) ? $_REQUEST['keywords'] : '';
        $this->pageTitle = 'Tim kiem: ' . $key;
        if (!empty($key)) {
            $criteria = new CDbCriteria();
            $criteria->condition = 'status=1 AND name LIKE "%' . $key . '%"';
            $criteria->order = 'rank DESC,id DESC';

            $count = Product::model()->count($criteria);
            $pages = new CPagination($count);
            //results per page
            $pages->pageSize = 15;
            $pages->applyLimit($criteria);
            $items = Product::model()->findAll($criteria);
            $number_result = count($items);
            $this->render('search', array(
                'items' => $items,
                'pages' => $pages,
                'number_result' => $number_result,
            ));
        } else {
            throw new CHttpException(400, 'Ban phai nhap tu khoa tim kiem');
        }
    }

    public function actionList($id)
    {
        $category = Category::model()->findByPk($id);
        $this->pageTitle = $category->name;
        $criteria = new CDbCriteria();
        $criteria->condition = 'status=1 AND categoryId=' . $category->id;
        $criteria->order = 'rank DESC,id DESC';

        $count = Product::model()->count($criteria);
        $pages = new CPagination($count);
        //results per page
        $pages->pageSize = 12;
        $pages->applyLimit($criteria);
        $items = Product::model()->findAll($criteria);

        $this->render('list', array(
            'items' => $items,
            'pages' => $pages,
            'category' => $category
        ));
        
        
    }

    public function actionDetail($id)
    {
        $sugest_products = Product::model()->findAll(array(
            'condition' => 'status=1 AND feature=1',
            'order' => 'rank DESC',
            'limit' => 3
        ));
        $model = Product::model()->findByPk($id);
        $this->pageTitle = $model->name;
        $criteria = new CDbCriteria();
        $criteria->condition = 'status=1 AND categoryId=' . $model->categoryId . ' AND id>' . $model->id;
        $criteria->limit = 8;
        $criteria->order = 'id DESC';
        $items = Product::model()->findAll($criteria);
        $info = Page::model()->findByUrl('thongtinmuahang');
        $info = isset($info) ? $info->content : '';
        $this->render('detail_new', array(
            'items' => $items,
            'model' => $model,
            'info' => $info,
            'sugest_products' => $sugest_products,
        ));
    }

    public function actionPage()
    {
        print_r($_POST['id']);
        die();
        if (empty($_GET['view']))
            $this->actionIndex();
        $model = Page::model()->findByUrl($_GET['view']);
// if page is not found, then run a controller with that name
        if ($model === NULL)
            throw new CHttpException(400, 'Khong tim thay trang');
        else {
            $this->pageTitle = $model->title;
            $this->render('page', array('model' => $model));
        }
    }

    public function actionContact()
    {
        $contact = new Contact();
        if (isset($_POST['submit'])) {
            $contact->full_name = $_POST['Contact']['full_name'];
            $contact->email = $_POST['Contact']['email'];
            $contact->content = $_POST['Contact']['content'];
            if ($contact->save()) {
                Yii::app()->user->setFlash('success', 'Cảm ơn bạn đã gửi thông tin, chúng tôi sẽ liên lạc với bạn trong thời gian sớm nhất!');
            }
        }
        $this->render('contact', array('model' => $contact));
    }

    /**
     * This is the action to handle external exceptions.
     */
    public function actionError()
    {
        if ($error = Yii::app()->errorHandler->error) {
            if (Yii::app()->request->isAjaxRequest)
                echo $error['message'];
            else
                $this->render('error', $error);
        }
    }
    

}